using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;

namespace BlazeApps.Library.Polls
{
    [Serializable()]
    public class PollAnswerChild : Csla.ReadOnlyBase<PollAnswerChild>
    {
        #region Business Properties and Methods

        //declare members
        private Guid _pollId = Guid.Empty;
        private Guid _answerId = Guid.NewGuid();
        private string _answerText = string.Empty;
        private decimal _answerCount = 0;

        public Guid PollId
        {
            get
            {
                CanReadProperty("PollId", true);
                return _pollId;
            }
        }

        [System.ComponentModel.DataObjectField(true, false)]
        public Guid AnswerId
        {
            get
            {
                CanReadProperty("AnswerId", true);
                return _answerId;
            }
        }

        public string AnswerText
        {
            get
            {
                CanReadProperty("AnswerText", true);
                return _answerText;
            }
        }

        public decimal AnswerCount
        {
            get
            {
                CanReadProperty("AnswerCount", true);
                return _answerCount;
            }
        }

        protected override object GetIdValue()
        {
            return _answerId;
        }

        #endregion //Business Properties and Methods

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in PollAnswerChild
            //AuthorizationRules.AllowRead("PollId", "PollAnswerChildReadGroup");
            //AuthorizationRules.AllowRead("AnswerId", "PollAnswerChildReadGroup");
            //AuthorizationRules.AllowRead("AnswerText", "PollAnswerChildReadGroup");
            //AuthorizationRules.AllowRead("AnswerCount", "PollAnswerChildReadGroup");

        }

        #endregion //Authorization Rules

        #region Factory Methods
        internal static PollAnswerChild GetPollAnswerChild(SafeDataReader dr)
        {
            return new PollAnswerChild(dr);
        }

        private PollAnswerChild(SafeDataReader dr)
        {
            Fetch(dr);
        }
        #endregion //Factory Methods

        #region Data Access

        #region Data Access - Fetch
        private void Fetch(SafeDataReader dr)
        {
            FetchObject(dr);

            //load child object(s)
            FetchChildren(dr);
        }

        private void FetchObject(SafeDataReader dr)
        {
            _pollId = dr.GetGuid("PollId");
            _answerId = dr.GetGuid("AnswerId");
            _answerText = dr.GetString("AnswerText");
            _answerCount = dr.GetDecimal("AnswerCount");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch
        #endregion //Data Access
    }
}
